package com.tang.Proxy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * @author tmh
 * @date 2024/3/13 22:32
 * @description
 */
@Component
public class RegisterBeanFactory implements BeanDefinitionRegistryPostProcessor {
    /**
     * 这个方法的作用就是把bean放到spring容器中去
     * @param registry
     * @throws BeansException
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        //1.new GenericBeanDefinition这个对象
        GenericBeanDefinition genericBeanDefinition=new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(ProxyFactoryBean.class);

        //2.创建一个BeanDefinitionHolder对象
        BeanDefinitionHolder definitionHolder=new BeanDefinitionHolder(genericBeanDefinition,"userDao");
        //3.调用registerBeanDefinition这个方法，第一个参数传入bean的名字，第二个参数调用definitionHolder这个对象的getBeanDefinition()方法
        registry.registerBeanDefinition("userDao", definitionHolder.getBeanDefinition());
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}
